//短信登录获取手机验证码
let sendSmsTime = 10
let t = null
$('.sms-login a').click(function () {
  let mobile = $('.mobile').val()
  if (!mobile || !/^1\d{10}$/.test(mobile)) {
   /*  swal({
      title: '请填写手机号',
      text: '美团提示!',
      icon: 'error',
      button: '确定!',
    }) */
    cocoMessage.error('请填写手机号');
    return
  }
  if (sendSmsTime != 10) return
  sendSmsTime--
  $(this).text(`重新获取(${sendSmsTime})`)
  t = setInterval(() => {
    if (sendSmsTime <= 1) {
      clearInterval(t)
      $(this).text('获取验证码')
      sendSmsTime = 10
      return
    }
    sendSmsTime--
    $(this).text(`重新获取(${sendSmsTime})`)
  }, 1000)

  $.post('/api/sms/send.jsp', { mobile }, res => {
    if (res.meta.state !== 201) {
      /* swal({
        title: res.meta.msg,
        text: '美团提示!',
        icon: 'error',
        button: '确定!',
      }) */
      cocoMessage.error(res.meta.msg);
    }
  }, 'json')
})
//短信登录 
//绑定点击事件 请求接口 失败弹窗提示 成功提示 判断勾选 重定向

jkCaptchaFn('#captcha2', captchaObj => {
  $('.sms').click(function (e) {
    e.preventDefault()
    if ($(this).index() != 6) return
    if (!captchaObj.getValidate()) {
     /*  swal({
        title: '行为验证码失败',
        text: '美团提示!',
        icon: 'error',
        button: '确定!',
      }) */
      cocoMessage.error('行为验证码失败');
      return
    }
    let mobile = $('.mobile').val()
    let code = $('.code').val()
    //if (!mobile || !code) return
    if (!mobile || !code) {
     /*  swal({
        title: '请填写表单数据',
        text: '美团提示!',
        icon: 'error',
        button: '确定!',
      }) */
      cocoMessage.error('请填写表单数据');
      return
    }
    $.post('/api/sms/login.jsp', { mobile, code }, res => {
      //console.log(res);
      if (res.meta.state === 200) {
       /*  swal({
          title: res.meta.msg,
          text: '美团提示!',
          icon: 'success',
          button: '确定!',
        }) */
        cocoMessage.success(res.meta.msg);
        if ($('#sms-remember').prop('checked')) {
          localStorage.setItem('token', res.data.token)
          localStorage.setItem('uname', res.data.uname)
          localStorage.setItem('expires', (new Date).getTime() + 1 * 1000 * 60 * 60 * 24 * 7)
        } else {
          sessionStorage.setItem('token', res.data.token)
          sessionStorage.setItem('uname', res.data.uname)
        }
        setTimeout(() => {
          location.href = './index.html'
          //history.back()
        }, 2000)
      } else {
       /*  swal({
          title: res.meta.msg,
          text: '美团提示!',
          icon: 'error',
          button: '确定!',
        }) */
        cocoMessage.error(res.meta.msg);
      }
    }, 'json')
  })
}, '280px')
